package com.ant.game.chess01;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Random;

public class ChessView  extends View{
	public static ChessView cv;
	private String chessView="chessView";
	private Context mContext;
	private Resources res;
	private Bitmap mBgImage;
	private TextView tv;
	String text="中国人你好";

	public int myColor=0;
	public static final int red=1;
	public static final int black=2;
	ArrayList<Coordinate> chesses;
	public int number=11;
	public int grid=35;


	private Coordinate cc;

	/////////////////////////////
	private int myCR=-1;//当前棋子所在的行
	private int myCC=-1;//所在的列
	private int mySR;//选中的行
	private int mySC;//选中的列
	private int g1;
	private int g;

	int[][] point=new int[10][9];
	int[] rowY=new int[10];
	int[] colX=new int[9];
	private boolean isComputer=false;
	private boolean isFirstChoice=false;
	private int r=7;
	private int c=4;
	Paint paint=new Paint();
	Paint paint1=new Paint();


	///////////////////////////////

	Bitmap[] chessImage=new Bitmap[14];

	public TextView getTv() {
		return tv;
	}

	public void setTv(TextView tv) {
		this.tv = tv;
	}



	public void setComputer(boolean isComputer) {
		this.isComputer = isComputer;
	}

	public void setMyColor(int myColor) {
		this.myColor = myColor;
	}

	public ChessView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	public ChessView(Context context, AttributeSet attrs) {

		super(context, attrs);
		cv=this;
		String con=context.toString();
		Log.i("context",con);
		mContext=context;
		res=context.getResources();
		paint.setARGB(100, 0, 255, 0);
		paint1.setARGB(100, 0, 0, 255);

		chesses=new ArrayList<Coordinate>();
		mBgImage=BitmapFactory.decodeResource(res, R.drawable.chessboard);
		chessImage[0]=BitmapFactory.decodeResource(res,R.drawable.a);
		chessImage[1]=BitmapFactory.decodeResource(res,R.drawable.b);
		chessImage[2]=BitmapFactory.decodeResource(res,R.drawable.c);
		chessImage[3]=BitmapFactory.decodeResource(res,R.drawable.d);
		chessImage[4]=BitmapFactory.decodeResource(res,R.drawable.e);
		chessImage[5]=BitmapFactory.decodeResource(res,R.drawable.f);
		chessImage[6]=BitmapFactory.decodeResource(res,R.drawable.g);
		chessImage[7]=BitmapFactory.decodeResource(res,R.drawable.a1);
		chessImage[8]=BitmapFactory.decodeResource(res,R.drawable.b2);
		chessImage[9]=BitmapFactory.decodeResource(res,R.drawable.c3);
		chessImage[10]=BitmapFactory.decodeResource(res,R.drawable.d4);
		chessImage[11]=BitmapFactory.decodeResource(res,R.drawable.e5);
		chessImage[12]=BitmapFactory.decodeResource(res,R.drawable.f6);
		chessImage[13]=BitmapFactory.decodeResource(res,R.drawable.g7);

		setFocusable(true);

	}

	public void initGame(){
		point=new int[10][9];
		chesses=new ArrayList<Coordinate>();
		init();
		initRowYAndRowX();
		initPoint();
	}
	public ChessView(Context context) {
		super(context);


	}



	@Override
	public void draw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.draw(canvas);
		canvas.drawBitmap(mBgImage,0,-50, null);
		//canvas.drawTextOnPath(text, path, hOffset, vOffset, paint);
		if(chesses != null && chesses.size() !=0 ){
			for(int i=0;i<=chesses.size()-1;i++){
				Coordinate c=chesses.get(i);
				if(myCC!=-1){
					if(c.x==colX[myCC]&&c.y==rowY[myCR]){
						canvas.drawBitmap(chessImage[c.imageIndex],c.x -15,c.y-15-50, null);
						canvas.drawCircle(c.x, c.y-50, 15, paint);//(c.x-15, c.y-15-50, c.x+15, c.y+15-50, )
					}else{
						canvas.drawBitmap(chessImage[c.imageIndex],c.x -15,c.y-15-50, null);
					}
				}else{
					canvas.drawBitmap(chessImage[c.imageIndex],c.x -15,c.y-15-50, null);
				}

			}
		}

		canvas.drawLine(colX[c]-15, rowY[r]-15-50, colX[c]+15, rowY[r]-15-50, paint1);//上
		canvas.drawLine(colX[c]-15, rowY[r]-15-50, colX[c]-15, rowY[r]+15-50, paint1);//左
		canvas.drawLine(colX[c]-15, rowY[r]+15-50, colX[c]+15, rowY[r]+15-50, paint1);//下
		canvas.drawLine(colX[c]+15, rowY[r]-15-50, colX[c]+15, rowY[r]+15-50, paint1);//右



	}




	public void initRowYAndRowX(){
		for(int i=0;i<10;i++){
			rowY[i]=100+i*grid;
		}
		for(int i=0;i<9;i++){
			colX[i]=20+i*grid;
		}

	}
	public void computerMoveChess(){
		if(isComputer){

			ArrayList<Coordinate> comChess;
			int y,x,n,m,pValue;
			comChess=new ArrayList<Coordinate>();
			if(myColor==red){
				for(int i=0;i<=chesses.size()-1;i++){
					Coordinate c=chesses.get(i);
					if(c.imageIndex<7){
						comChess.add(c);
					}
				}
			}else{
				for(int i=0;i<=chesses.size()-1;i++){
					Coordinate c=chesses.get(i);
					if(c.imageIndex>=7){
						comChess.add(c);
					}
				}
			}

			Random r=new Random();
			Coordinate cc;
			if(comChess.size()==1){
				cc=comChess.get(0);
			}else{
				cc=comChess.get(r.nextInt(comChess.size()));
			}
			n=(cc.y-100)/grid;
			m=(cc.x-20)/grid;
			pValue=point[n][m];
			while(true){
				y=r.nextInt(10);
				x=r.nextInt(9);
				if(cc.imageIndex==6 || cc.imageIndex==13){
					if (point[y][x] == 15) {
						theRuleOfComputerZu(m, n,x,y);
					} else {
						if ((point[y][x] / 7) != (point[n][m] / 7))// 当然，想吃的子不能是自己的
						{
							theRuleOfComputerZu(m, n, x, y);
						}
					}
				}else{
					computerSwitch(n,m,y,x);
				}
				if(point[n][m]!=pValue){
					invalidate();
					isComputer=false;

					break;
				}
			}

		}

	}





	private void makeTextToast(String str){
		Toast.makeText(this.mContext,str,Toast.LENGTH_SHORT).show();
	}
	public void computerSwitch(int n,int m,int y,int x){

		moveChess(n,m,y,x);
	}
	public void initPoint(){

		for(int i=0;i<10;i++){
			for(int j=0;j<9;j++){
				point[i][j]=15;
			}
		}

		for(int i=0;i<=chesses.size()-1;i++){
			Coordinate c=chesses.get(i);
			point[(c.y-100)/grid][(c.x-20)/grid]=c.imageIndex;

		}

	}





	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch(keyCode){
			case KeyEvent.KEYCODE_DPAD_RIGHT:
				c+=1;
				if(c>8)
					c=8;
				invalidate();
				break;
			case KeyEvent.KEYCODE_DPAD_LEFT:
				c-=1;
				if(c<0)
					c=0;
				invalidate();
				break;
			case KeyEvent.KEYCODE_DPAD_UP:
				r-=1;
				if(r<0)
					r=0;
				invalidate();
				break;
			case KeyEvent.KEYCODE_DPAD_DOWN:
				r+=1;
				if(r>9)
					r=9;
				invalidate();
				break;

			case KeyEvent.KEYCODE_DPAD_CENTER:
				touch_start(colX[c],rowY[r]);
				checkWin(1);
				computerMoveChess();
				checkWin(2);
				break;
		}



		return true;
	}

	private void touch_start(int x,int y){

		if(!isComputer){
			if(isFirstChoice==false){
				if(choice(x,y)){//选择是我方的棋子
					int[] numYandX=pointCheck(x,y);
					myCR=numYandX[0];
					myCC=numYandX[1];
					isFirstChoice=true;
					invalidate();

					Log.i("touch"," isFirstChoice "+isFirstChoice);
				}else{
					//请用户选择自己的棋子
					makeTextToast("请你选择自己的棋子!");
				}
			}else if(isFirstChoice==true){
				if(choice(x,y)){//选择是我方的棋子
					int[] numYandX=pointCheck(x,y);
					myCR=numYandX[0];
					myCC=numYandX[1];
					Log.i("touch",this+" ");
					isFirstChoice=true;
					invalidate();
					Log.i("touch",this+" ");

					Log.i("touch"," isFirstChoice "+isFirstChoice);


				}else{//不是我方的棋子
					int[] numYandX=pointCheck(x,y);
					mySR=numYandX[0];
					mySC=numYandX[1];
					int animalIndex=point[myCR][myCC];
					moveChess(myCR,myCC,mySR,mySC);
					if(point[myCR][myCC]!=animalIndex){//成功走子
						//应该计算机,走棋了
						//;
						myCC=-1;
						invalidate();
						Log.i("touch",this+" ");
						invalidate();
						Log.i("touch",this+" ");
						isFirstChoice=false;
						isComputer=true;
						Log.i("touch"," isComputer "+isComputer);
						//objHandler.sendMessage(objHandler.obtainMessage());
					}else{
						myCC=-1;
						//提示用户你的走法不合象棋规则
						isFirstChoice=false;
						invalidate();
						makeTextToast("走法不合象棋规则!");


					}
				}
			}


		}

	}
	private void checkWin(int m){
		switch(m){
			case 1:
				if(myColor==red){
					for(int i=0;i<3;i++){
						for(int j=0;j<3;j++){
							if(point[i][j+3]!=0){
								continue;
							}else{
								return;
							}
						}
					}
					showWinDialog("恭喜你","你赢了这一局!");

				}else if(myColor==black){
					for(int i=0;i<3;i++){
						for(int j=0;j<3;j++){
							if(point[i][j+3]!=7){
								continue;
							}else{
								return;
							}
						}
					}
					showWinDialog("恭喜你","你赢了这一局!");

				};
				break;
			case 2:
				if(myColor==red){
					for(int i=0;i<3;i++){
						for(int j=0;j<3;j++){
							if(point[i+7][j+3]!=7){
								continue;
							}else{
								return;
							}
						}
					}

					showWinDialog("很遗憾","你输了这一局!");

				}else if(myColor==black){
					for(int i=0;i<3;i++){
						for(int j=0;j<3;j++){
							if(point[i+7][j+3]!=0){
								continue;
							}else{
								return;
							}
						}
					}
					showWinDialog("很遗憾","你输了这一局!");

				};


				break;
		}





	}

	private void showWinDialog(String title,String message){
		new AlertDialog.Builder(this.mContext)
				.setTitle(title)
				.setMessage(message)
				.setPositiveButton("确定", new DialogInterface.OnClickListener(){
					public void onClick(DialogInterface dialog, int which) {

					}




				}).show();

	}






	//选择我方的棋子
	private boolean choice(int x,int y){
		Log.i(chessView,"choice start ");
		for(int i=0;i<=chesses.size()-1;i++){
			Log.i(chessView,"choice start 2");
			cc=chesses.get(i);
			if(cc.x+15>x && cc.x-15<x && cc.y+15>y && cc.y-15<y){
				if(cc.myColor==myColor){
					Log.i(chessView,"choice "+cc.x+" "+cc.y+" "+x+" "+y);
					return true;
				}
				//请选择自己的棋子,
				//return false;
			}
			Log.i(chessView,"choice start 3 ");
		}
		//提示用户选择棋子,
		return false;

	}
	public int[] pointCheck(int x,int y){
		int[] numYandX=new int[2];
		int numY=20,numX=20;
		for(int i=0;i<10;i++){
			if(rowY[i]+15>y && rowY[i]-15<y){
				numY=i;
				break;
			}
		}
		for(int i=0;i<9;i++){
			if(colX[i]+15>x && colX[i]-15<x){
				numX=i;
				break;
			}
		}
		if(numY !=20 && numX != 20){
			numYandX[0]=numY;
			numYandX[1]=numX;

		}
		return numYandX;
	}
	private void theRuleOfChe(int m, int n, int sx, int sy) {// 车的规则
		int i;
		g = 0;
		if (m == sx) {
			if (n > sy) {
				for (i = 1; i < n - sy; i++) {
					if (point[sy + i][m] != 15) {//中间有棋子隔着
						g++;
					}
				}
			} else {
				for (i = 1; i < sy - n; i++) {
					if (point[n + i][m] != 15) {
						g++;
					}
				}
			}
			if (g == 0) {//没有棋子隔着,交换
				changTwoChessNum(m, n, sx, sy, true);
			}

		}
		if (n == sy) {
			if (m > sx) {
				for (i = 1; i < m - sx; i++) {
					if (point[n][i + sx] != 15) {
						g++;
					}
				}
			} else {
				for (i = 1; i < sx - m; i++) {
					if (point[n][m + i] != 15) {
						g++;
					}
				}
			}
			if (g == 0) {
				changTwoChessNum(m, n, sx, sy, true);
			}

		}
	}private void theRuleOfMa(int m, int n, int sx, int sy) {// 马的规则
		//下右
		if (n < 9) {
			if (point[n + 1][m] == 15) {//没有踩马脚
				if (sx - m == 1) {
					if (sy - n == 2) {
						changTwoChessNum(m, n, sx, sy, true);
					}
				}
			}
		}
		//上左
		if (n > 0) {
			if (point[n - 1][m] == 15) {
				if (m - sx == 1) {
					if (n - sy == 2) {
						changTwoChessNum(m, n, sx, sy, true);
					}
				}
			}
		}
		//下左
		if (n < 9) {
			if (point[n + 1][m] == 15) {
				if (sx - m == -1) {
					if (sy - n == 2) {
						changTwoChessNum(m, n, sx, sy, true);
					}
				}
			}
		}
		//上左
		if (n > 0) {
			if (point[n - 1][m] == 15) {
				if (m - sx == -1) {
					if (n - sy == 2) {
						changTwoChessNum(m, n, sx, sy, true);
					}
				}
			}
		}
		if (m < 8) {
			if (point[n][m + 1] == 15) {
				if (sx - m == 2) {
					if (sy - n == 1) {
						changTwoChessNum(m, n, sx, sy, true);
					}
				}
			}
		}
		if (m > 0) {
			if (point[n][m - 1] == 15) {
				if (m - sx == 2) {
					if (n - sy == 1) {
						changTwoChessNum(m, n, sx, sy, true);
					}
				}
			}
		}
		if (m < 8) {
			if (point[n][m + 1] == 15) {
				if (sx - m == 2) {
					if (sy - n == -1) {
						changTwoChessNum(m, n, sx, sy, true);
					}
				}
			}
		}
		if (m > 0) {
			if (point[n][m - 1] == 15) {
				if (m - sx == 2) {
					if (n - sy == -1) {
						changTwoChessNum(m, n, sx, sy, true);
					}
				}
			}
		}
	}
	private void theRuleOfPao(int m, int n, int sx, int sy, int g1) {// 炮的规则
		int i;
		g = 0;
		if (m == sx) {
			if (n > sy) {
				for (i = 1; i < n - sy; i++) {
					if (point[sy + i][m] != 15) {//直线方向有子
						g++;
					}
				}
			} else {
				for (i = 1; i < sy - n; i++) {
					if (point[n + i][m] != 15) {
						g++;
					}
				}
			}
			if (g == g1) {
				changTwoChessNum(m, n, sx, sy, true);
			}

		}
		if (n == sy) {
			if (m > sx) {
				for (i = 1; i < m - sx; i++) {
					if (point[n][i + sx] != 15) {
						g++;
					}
				}
			} else {
				for (i = 1; i < sx - m; i++) {
					if (point[n][m + i] != 15) {
						g++;
					}
				}
			}
			if (g == g1) {
				changTwoChessNum(m, n, sx, sy, true);
			}

		}
	}
	private void theRuleOfXiang(int m, int n, int sx, int sy) {// 相的规则
		if((n<=4 && sy<=4)||(n>=5 && sy>=5)){

		}else{
			return;
		}



		if (n < 9 & m < 8) {
			if (point[n + 1][m + 1] == 15) {
				if ((sx - m == 2) & (sy - n == 2)) {
					changTwoChessNum(m, n, sx, sy, true);
				}
			}
		}
		if (n > 0 & m < 8) {
			if (point[n - 1][m + 1] == 15) {
				if ((sx - m == 2) & (sy - n == -2)) {
					changTwoChessNum(m, n, sx, sy, true);
				}
			}
		}
		if (n < 9 & m > 0) {
			if (point[n + 1][m - 1] == 15) {
				if ((sx - m == -2) & (sy - n == 2)) {
					changTwoChessNum(m, n, sx, sy, true);
				}
			}
		}
		if (n > 0 & m > 0) {
			if (point[n - 1][m - 1] == 15) {
				if ((sx - m == -2) & (sy - n == -2)) {
					changTwoChessNum(m, n, sx, sy, true);
				}
			}
		}
	}
	private void theRuleOfShi(int m, int n, int sx, int sy) {// 士的规则
		if ((m > 2 & m < 6) & (sx > 2 & sx < 6) & (n >= 7 & n <= 9)
				& (sy >= 7 & sy <= 9)) {

			if ((sx - m == 1) & (sy - n == 1)) {
				changTwoChessNum(m, n, sx, sy, true);
			}

			if ((sx - m == 1) & (sy - n == -1)) {
				changTwoChessNum(m, n, sx, sy, true);
			}
			if ((sx - m == -1) & (sy - n == 1)) {
				changTwoChessNum(m, n, sx, sy, true);
			}
			if ((sx - m == -1) & (sy - n == -1)) {
				changTwoChessNum(m, n, sx, sy, true);
			}
		}
		if ((m > 2 & m < 6) & (sx > 2 & sx < 6) & (n >= 0 & n < 3)
				& (sy >= 0 & sy < 3)) {
			if ((sx - m == 1) & (sy - n == 1)) {
				changTwoChessNum(m, n, sx, sy, true);
			}
			if ((sx - m == 1) & (sy - n == -1)) {
				changTwoChessNum(m, n, sx, sy, true);
			}
			if ((sx - m == -1) & (sy - n == 1)) {
				changTwoChessNum(m, n, sx, sy, true);
			}
			if ((sx - m == -1) & (sy - n == -1)) {
				changTwoChessNum(m, n, sx, sy, true);
			}
		}
	}
	private void theRuleOfShuai(int m, int n, int sx, int sy) {// 帅的规则
		if ((m > 2 & m < 6) & (sx > 2 & sx < 6) & (n >= 7 & n <= 9)
				& (sy >= 7 & sy <= 9)) {
			if ((sx - m == 1) & (sy - n == 0)) {
				changTwoChessNum(m, n, sx, sy, true);
			}
			if ((sx - m == -1) & (sy - n == 0)) {
				changTwoChessNum(m, n, sx, sy, true);
			}
			if ((sx - m == 0) & (sy - n == 1)) {
				changTwoChessNum(m, n, sx, sy, true);
			}
			if ((sx - m == 0) & (sy - n == -1)) {
				changTwoChessNum(m, n, sx, sy, true);
			}
		}
		if ((m > 2 & m < 6) & (sx > 2 & sx < 6) & (n >= 0 & n < 3)
				& (sy >= 0 & sy < 3)) {
			if ((sx - m == 1) & (sy - n == 0)) {
				changTwoChessNum(m, n, sx, sy, true);
			}
			if ((sx - m == -1) & (sy - n == 0)) {
				changTwoChessNum(m, n, sx, sy, true);
			}
			if ((sx - m == 0) & (sy - n == 1)) {
				changTwoChessNum(m, n, sx, sy, true);
			}
			if ((sx - m == 0) & (sy - n == -1)) {
				changTwoChessNum(m, n, sx, sy, true);
			}
		}
	}
	private void theRuleOfComputerZu(int m, int n, int sx, int sy){
		if (sy >= n) {
			if (n < 5) {
				if ((sy - n == -1) & (sx - m == 0)) {
					changTwoChessNum(m, n, sx, sy, true);
				}
			} else {
				if ((sy - n == -1) & (sx - m == 0)) {
					changTwoChessNum(m, n, sx, sy, true);
				}
				if ((sy - n == 0) & (sx - m == 1)) {
					changTwoChessNum(m, n, sx, sy, true);
				}
				if ((sy - n == 0) & (sx - m == -1)) {
					changTwoChessNum(m, n, sx, sy, true);
				}
			}
		}
	}
	private void theRuleOfZu(int m, int n, int sx, int sy) {// 卒的规则

		if (sy <= n) {
			if (n > 4) {
				if ((sy - n == -1) & (sx - m == 0)) {
					changTwoChessNum(m, n, sx, sy, true);
				}
			} else {
				if ((sy - n == -1) & (sx - m == 0)) {
					changTwoChessNum(m, n, sx, sy, true);
				}
				if ((sy - n == 0) & (sx - m == 1)) {
					changTwoChessNum(m, n, sx, sy, true);
				}
				if ((sy - n == 0) & (sx - m == -1)) {
					changTwoChessNum(m, n, sx, sy, true);
				}
			}
		}
	}




	protected void changTwoChessNum(int m, int n, int selectedX, int selectedY,
									boolean send) {// 改变两个格子的值


		if(point[selectedY][selectedX]!=15){
			for(int i=0;i<chesses.size()-1;i++){
				Coordinate c=chesses.get(i);
				if(c.x==colX[selectedX]&&c.y==rowY[selectedY]){
					chesses.remove(i);

				}
			}

		}
		for(int i=0;i<chesses.size()-1;i++){
			Coordinate c=chesses.get(i);
			if(c.x==colX[m]&&c.y==rowY[n]){
				c.y=rowY[selectedY];
				c.x=colX[selectedX];
				point[selectedY][selectedX]=c.imageIndex;
				point[n][m]=15;
			}
		}




	}


	public void moveChess(int n,int m,int selectedY, int selectedX){
		// 一个棋子，又不想选了，这只需什么都不做
		if ((point[n][m] == 4) | (point[n][m] == 11))// 当选定的棋子是车的时候
		{ // repaint就OK了
			if (point[selectedY][selectedX] == 15)// 当下一步走的是空格，则改变选种的格子和下一步所
			{ // 走的格子的point[][]和word[][]的植，然后repaint就OK
				theRuleOfChe(m, n, selectedX, selectedY);
			} else// 当下一步是想吃对方的子的，则把下一步格子的值变为刚才选定的格子的值，而
			{ // 刚才选定的格子的值则便为零

				if ((point[selectedY][selectedX] / 7) != (point[n][m] / 7))//表示吃的不是自己的棋子
				{
					theRuleOfChe(m, n, selectedX, selectedY);

				}
			}
		}
		if ((point[n][m] == 3) | (point[n][m] == 10))// 当选定的棋子是马的时候
		{
			if (point[selectedY][selectedX] == 15) {
				theRuleOfMa(m, n, selectedX, selectedY);
			} else {
				if ((point[selectedY][selectedX] / 7) != (point[n][m] / 7))// 当然，想吃的子不能是自己的
				{
					theRuleOfMa(m, n, selectedX, selectedY);

				}
			}
		}
		if ((point[n][m] == 5) | (point[n][m] == 12))// 当选定的棋子是炮的时候
		{
			if (point[selectedY][selectedX] == 15) {
				g1 = 0;
				theRuleOfPao(m, n, selectedX, selectedY, g1);
			} else {
				g1 = 1;
				if ((point[selectedY][selectedX] / 7) != (point[n][m] / 7))// 当然，想吃的子不能是自己的
				{
					theRuleOfPao(m, n, selectedX, selectedY, g1);

				}
			}
		}
		if ((point[n][m] == 2) | (point[n][m] == 9))// 当选定的棋子是相的时候
		{
			if (point[selectedY][selectedX] == 15) {
				theRuleOfXiang(m, n, selectedX, selectedY);
			} else {
				if ((point[selectedY][selectedX] / 7) != (point[n][m] / 7))// 当然，想吃的子不能是自己的
				{
					theRuleOfXiang(m, n, selectedX, selectedY);

				}
			}
		}
		if ((point[n][m] == 1) | (point[n][m] == 8)
				| (point[n][m] == 20) | (point[n][m] == 22))// 当选定的棋子是士的时候
		{
			if (point[selectedY][selectedX] == 15) {
				theRuleOfShi(m, n, selectedX, selectedY);
			} else {
				if ((point[selectedY][selectedX] / 7) != (point[n][m] / 7))// 当然，想吃的子不能是自己的
				{
					theRuleOfShi(m, n, selectedX, selectedY);

				}
			}
		}
		if ((point[n][m] == 0) | (point[n][m] == 7))// 当选定的棋子是帅的时候
		{
			if (point[selectedY][selectedX] == 15) {
				theRuleOfShuai(m, n, selectedX, selectedY);
			} else {
				if ((point[selectedY][selectedX] / 7) != (point[n][m] / 7))// 当然，想吃的子不能是自己的
				{
					theRuleOfShuai(m, n, selectedX, selectedY);

				}
			}
		}
		if (point[n][m] == 13 | point[n][m] == 6)// 当选定的棋子是红方兵的时候
		{
			if (point[selectedY][selectedX] == 15) {
				theRuleOfZu(m, n, selectedX, selectedY);
			} else {
				if ((point[selectedY][selectedX] / 7) != (point[n][m] / 7))// 当然，想吃的子不能是自己的
				{
					theRuleOfZu(m, n, selectedX, selectedY);
				}
			}


		}



	}





	@Override
	public boolean onTouchEvent(MotionEvent event) {
		float x=event.getX();
		float y=event.getY();
		switch(event.getAction()){
			case MotionEvent.ACTION_DOWN:
				touch_start((int)x,(int)y+50);
				computerMoveChess();
				break;
		}
		return true;
	}


	//初始化棋盘上,有棋子的坐标与图片索引

	public void init(){
		if(myColor==red){
			number=11;
			for(int i=0;i<=8;i++){
				if(i<4){
					chesses.add(new Coordinate(20+i*grid,415,red,number));
					chesses.add(new Coordinate(20+i*grid,415-9*grid,black,number-7));
					number--;
				}else{
					chesses.add(new Coordinate(20+i*grid,415,red,number));
					chesses.add(new Coordinate(20+i*grid,415-9*grid,black,number-7));
					number++;
				}
			}
			number=12;
			for(int i=0;i<=1;i++){
				if(i==0){
					chesses.add(new Coordinate(55+i*grid,415-2*grid,red,number));
					chesses.add(new Coordinate(55+i*grid,415-7*grid,black,number-7));
				}else{
					chesses.add(new Coordinate(55+6*grid,415-2*grid,red,number));
					chesses.add(new Coordinate(55+6*grid,415-7*grid,black,number-7));
				}
			}
			number=13;
			for(int i=0;i<=4;i++){
				chesses.add(new Coordinate(20+2*i*grid,415-3*grid,red,number));
				chesses.add(new Coordinate(20+2*i*grid,415-6*grid,black,number-7));

			}


		}else{
			number=4;
			for(int i=0;i<=8;i++){
				if(i<4){
					chesses.add(new Coordinate(20+i*grid,415,black,number));
					chesses.add(new Coordinate(20+i*grid,415-9*grid,red,number+7));
					number--;
				}else{
					chesses.add(new Coordinate(20+i*grid,415,black,number));
					chesses.add(new Coordinate(20+i*grid,415-9*grid,red,number+7));
					number++;
				}
			}
			number=5;
			for(int i=0;i<=1;i++){
				if(i==0){
					chesses.add(new Coordinate(55+i*grid,415-2*grid,black,number));
					chesses.add(new Coordinate(55+i*grid,415-7*grid,red,number+7));
				}else{
					chesses.add(new Coordinate(55+6*grid,415-2*grid,black,number));
					chesses.add(new Coordinate(55+6*grid,415-7*grid,red,number+7));
				}
			}
			number=6;
			for(int i=0;i<=4;i++){
				chesses.add(new Coordinate(20+2*i*grid,415-3*grid,black,number));
				chesses.add(new Coordinate(20+2*i*grid,415-6*grid,red,number+7));

			}
		}
	}
	class Coordinate{
		public int x;
		public int y;
		public int myColor;
		public int imageIndex;
		public Coordinate(int x, int y, int myColor, int imageIndex) {
			super();
			this.x = x;
			this.y = y;
			this.myColor = myColor;
			this.imageIndex = imageIndex;
		}


	}



}
